import { getLogin,getInfo } from "../api/user";
import { setToken,getToken,removeToken } from "../utils/Cookie";
export default {
    namespaced:true,
    state:{
        id:0,
        name:'',
        roles:'',
        token:'',
        msg:'',
        avatar:''
    },
    mutations:{
        setId(state,id){
            state.id=id
        },
        setName(state,name){
            state.name=name
        },
        setRoles(state,roles){
            state.roles=roles
        },
        setTokenFun(state,token){
            state.token=token
        },
        setMsg(state,msg){
            state.msg=msg
        },
        setAvatar(state,avatar){
            state.avatar=avatar
        },
        clearId(state){
            state.id=''
        }
    },
    actions:{
        async LoginFun({dispatch,commit},form){
            let res = await getLogin(form)
            // console.log(res.data.data);
            if (res.data.msg == '数据请求失败') {
                commit('setMsg', res.data.msg)
            } else {
                const token = res.data.data[0].token
                setToken(token)
            }
            return res.data
        },
        async InfoFun({commit},token){
            let res=await getInfo({token})
            commit('setId',res.data.data[0].id)
            commit('setName',res.data.data[0].name)
            commit('setRoles',res.data.data[0].roles)
            commit('setAvatar',res.data.data[0].avatar)
            commit('setTokenFun',token)
            return res
        },
    }
}